package com.cskaoyan.controller;


import com.cskaoyan.bean.vo.OrderCommentParameter;
import com.cskaoyan.bean.data.OrderDetailData;
import com.cskaoyan.bean.data.PageData;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.model.MarketOrderGoods;
import com.cskaoyan.service.OrderService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("wx/order")
public class WxOrderController {
    @Autowired
    OrderService orderService;

    @PostMapping("submit") //提交订单
    public BaseRespVo submit(@RequestBody Map<String, Object> map) {
        //获取userId
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()) {
            Integer userId = (Integer) subject.getPrincipal();

            //调用submit方法得到响应数据
            Map dataMap = orderService.submit(userId, map);
            if (dataMap == null) {
                //如果响应的dataMap为null说明库存不足
                return BaseRespVo.fail("库存不足", 711);
            }
            //返回给前端数据
            return BaseRespVo.ok(dataMap);
        } else {
            return BaseRespVo.unAuthc();
        }
    }

    @GetMapping("list") //订单列表
    @RequiresPermissions("orderlist")
    public BaseRespVo list(Integer showType, Integer page, Integer limit) {
        //获取userId
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()) {
            Integer userId = (Integer) subject.getPrincipal();

            //调用list方法得到封装响应数据的dataList
            PageData data = orderService.list(userId, page, limit, showType);
            //封装页面信息
            //PageData data = PageData.list(dataList);
            //返回给前端响应数据
            return BaseRespVo.ok(data);
        } else {
            return BaseRespVo.unAuthc();
        }
    }

    @GetMapping("detail") //订单详情
    public BaseRespVo detail(Integer orderId) {
        //调用detail方法得到响应数据
        OrderDetailData data = orderService.detail(orderId);
        //返回给前端响应数据
        return BaseRespVo.ok(data);
    }

    @PostMapping("prepay") //订单的预支付会话
    public BaseRespVo prepay(@RequestBody Map<String, String> map) {
        //从map中获得前端传来的orderId
        String orderId = map.get("orderId");
        //调用prepay进行预支付处理
        orderService.prepay(Integer.parseInt(orderId));
        //返回成功结果
        return BaseRespVo.ok(null);
    }

    @PostMapping("refund") //退款取消订单
    public BaseRespVo refund(@RequestBody Map<String, Integer> map) {
        //从map中获得前端传来的orderId
        Integer orderId = map.get("orderId");
        //调用refund进行退款处理
        orderService.refund(orderId);
        //返回成功结果
        return BaseRespVo.ok(null);
    }

    @PostMapping("cancel") //取消订单
    public BaseRespVo cancel(@RequestBody Map<String, Integer> map) {
        //从map中获得前端传来的orderId
        Integer orderId = map.get("orderId");
        //调用cancel处理订单的取消
        orderService.cancel(orderId);
        //返回成功结果
        return BaseRespVo.ok(null);
    }

    @PostMapping("delete") //删除订单
    public BaseRespVo delete(@RequestBody Map<String, Integer> map) {
        //从map中获得前端传来的orderId
        Integer orderId = map.get("orderId");
        //调用delete进行订单的删除
        orderService.delete(orderId);
        //返回成功结果
        return BaseRespVo.ok(null);
    }

    @PostMapping("confirm") //确认收货
    public BaseRespVo confirm(@RequestBody Map<String, Integer> map) {
        //从map中获得前端传来的orderId
        Integer orderId = map.get("orderId");
        //调用confirm方法处理确认收货操作
        orderService.confirm(orderId);
        //返回成功结果
        return BaseRespVo.ok(null);
    }

    @GetMapping("goods") //待评价商品信息
    public BaseRespVo goods(Integer orderId, Integer goodsId) {
        //调用goods方法得到封装了响应字段的orderGoods对象
        MarketOrderGoods orderGoods = orderService.goods(orderId, goodsId);
        if (orderGoods == null) {
            //如果返回的是null，数据库数据有问题(重复订单或者订单被删了)
            return BaseRespVo.fail("系统内部错误", 502);
        }
        //返回查询到的订单商品信息
        return BaseRespVo.ok(orderGoods);
    }

    @PostMapping("comment") //评价订单商品信息
    public BaseRespVo comment(@RequestBody OrderCommentParameter parameter) {
        //获取userId
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()) {
            Integer userId = (Integer) subject.getPrincipal();

            //调用comment方法
            orderService.comment(parameter, userId);
            //返回成功结果
            return BaseRespVo.ok(null);
        } else {
            return BaseRespVo.unAuthc();
        }
    }
}
